package com.itboye.jmsfhz.main;

import java.text.SimpleDateFormat;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.ui.web.WebActivity;
import com.itboye.jmsfhz.app.ui.web.WebProjectfaremgnAvtivity;
import com.itboye.jmsfhz.data.model.ProjectBean;
import com.itboye.jmsfhz.data.presenter.Projectpresenter;
import com.itboye.jmsfhz.widget.EditTextWithDelete;
import com.itboye.jmsfhz.widget.XListView;
import com.itboye.jmsfhz.widget.XListView.IXListViewListener;

/***
 * 项目
 * 
 * @author dsawe
 * 
 */
@SuppressLint("SimpleDateFormat")
public class ProjectFragment extends BaseFragment implements Observer,
		IXListViewListener, TextWatcher {

	XListView listviewProject;
	View view;
	ProjectBean bean;
	Projectpresenter projectpresenter;
	int page_size = 5;
	int page_num = 1;
	AdapterView adapterView;
	TextView txt_title;
	String uid;
	EditText editextproject;
	ListView listview;
	Handler handler;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_project;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		handler = new Handler();
		IntentFilter intentFilterAvart2 = new IntentFilter("changeavart");
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart2);

		IntentFilter intentFilterAvart3 = new IntentFilter("project");
		getActivity().registerReceiver(start, intentFilterAvart3);
		
		uid = (String) SPUtils.get(getActivity(), null, SpPublicParams.UID, "");
		onProjectGetInfo();
		if (uid == null || uid == "") {
			editextproject.setVisibility(View.GONE);
			listviewProject.setVisibility(View.GONE);
			listview.setVisibility(View.GONE);
		} else {
			editextproject.setVisibility(View.VISIBLE);
			listviewProject.setVisibility(View.VISIBLE);
			listview.setVisibility(View.VISIBLE);
			onProjectGetInfo();

		}

		if (page_size <= 5) {
			/** 刷新列表 ***/
			listviewProject.setXListViewListener(this);
			/** 上拉加载 **/
			listviewProject.setPullLoadEnable(false);
			/** 刷新 **/
			listviewProject.setPullRefreshEnable(true);
		} else if (page_size >= 5) {
			/** 刷新列表 ***/
			listviewProject.setXListViewListener(this);
			/** 上拉加载 **/
			listviewProject.setPullLoadEnable(true);
			/** 刷新 **/
			listviewProject.setPullRefreshEnable(true);
		}
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(android.widget.AdapterView<?> parent,
					View view, int position, long id) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(getActivity(),
						WebProjectfaremgnAvtivity.class);
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Project/detail?id="
								+ bean.getResult().get(position).getId());

				intent.putExtra("title", "项目详情");
				startActivity(intent);
			}
		});
		listviewProject.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(android.widget.AdapterView<?> parent,
					View view, int position, long id) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(getActivity(),
						WebProjectfaremgnAvtivity.class);
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Project/detail?id="
								+ bean.getResult().get(position - 1).getId());
				intent.putExtra("id", bean.getResult().get(position - 1)
						.getId());
				System.out.println("hahahahah"+bean.getResult().get(position - 1)
						.getId());
				intent.putExtra("title", "项目详情");
				startActivity(intent);
			}
		});
	}

	public void onProjectGetInfo() {
		bean = new ProjectBean();
		projectpresenter = new Projectpresenter(this);
		bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		bean.setUid(uid);// (uid);
		projectpresenter.getInfo(bean);
		editextproject.addTextChangedListener(this);
	}

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {

			listviewProject.setVisibility(View.VISIBLE);
			listview.setVisibility(View.VISIBLE);
			editextproject.setVisibility(View.VISIBLE);

		};
	};
	BroadcastReceiver start = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			editextproject.setVisibility(View.GONE);
			listviewProject.setVisibility(View.GONE);
			listview.setVisibility(View.GONE);

		};
	};

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity model = super.handlerError(data);

		if (model.getEventType() == projectpresenter.ProjectSuccessEvent) {
			bean = (ProjectBean) model.getData();

			adapterView = new AdapterView(getActivity(), bean);
			listviewProject.setAdapter(adapterView);

		} else if (model.getEventType() == projectpresenter.ProjectKeySuccessEvent) {
			bean = (ProjectBean) model.getData();

			adapterView = new AdapterView(getActivity(), bean);
			listview.setAdapter(adapterView);

		}

		try {
			/**
			 * 当数据加载出来后dialog则消失
			 */
			closeProgressDialog();
		} catch (Exception e) {
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>加载完毕"
					+ model.getData());
		}

		stopLoad();

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_num = 1;
		// bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		page_size = 10;
		projectpresenter.getInfo(bean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_num++;
		// bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		page_size++;
		projectpresenter.getInfo(bean);
	}

	public void stopLoad() {
		listviewProject.stopLoadMore();
		listviewProject.stopRefresh();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (editextproject.getText().toString().length() > 0) {
			try {
				if (uid == null || uid == "") {
					listview.setVisibility(View.GONE);
					listviewProject.setVisibility(View.GONE);
				} else {
					listview.setVisibility(View.VISIBLE);
					listviewProject.setVisibility(View.GONE);
					bean.setPage_num(page_num + "");
					bean.setPage_size(page_size + "");
					bean.setUid(uid);// (uid);
					bean.setKeyword(editextproject.getText().toString() + "");
					projectpresenter.add(bean);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		} else {
			listview.setVisibility(View.GONE);
			listviewProject.setVisibility(View.VISIBLE);
			bean.setPage_num(page_num + "");
			bean.setPage_size(page_size + "");
			bean.setUid(uid);// (uid);
			projectpresenter.getInfo(bean);
		}
	}

}

class AdapterView extends BaseAdapter {
	Context context;
	ProjectBean list;

	public AdapterView(Context context, ProjectBean list) {
		this.context = context;
		this.list = list;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.getResult() == null ? 0 : list.getResult().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		viewHoder hoder;
		if (convertView == null) {
			hoder = new viewHoder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_project, null);
			hoder.tvprojectcompany = (TextView) convertView
					.findViewById(R.id.tvprojectcompany);
			hoder.tvprojectCount = (TextView) convertView
					.findViewById(R.id.tvprojectCount);
			hoder.tvEmplomenttionTime = (TextView) convertView
					.findViewById(R.id.tvEmplomenttionTime);
			hoder.tvprojecttextView = (TextView) convertView
					.findViewById(R.id.tvprojecttextView);
			convertView.setTag(hoder);
		} else {
			hoder = (viewHoder) convertView.getTag();
		}

		hoder.tvprojectcompany.setText(list.getResult().get(position)
				.getTitle());
		hoder.tvprojectCount.setText(list.getResult().get(position)
				.getCome_from());
		hoder.tvEmplomenttionTime.setText(new SimpleDateFormat("yyyy-MM-dd")
				.format(Long.parseLong(list.getResult().get(position)
						.getCreate_time()) * 1000));
		hoder.tvprojecttextView.setText(list.getResult().get(position)
				.getSummary());
		return convertView;
	}

	class viewHoder {
		TextView tvprojectcompany;
		TextView tvprojectCount;
		TextView tvEmplomenttionTime;
		TextView tvprojecttextView;
	}

}
